package com.util.thread;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author CHL
 * @ClassName: ThreadPoolUtils
 * @Description: TODO
 * @date 2023/5/8 10:10
 * @Version 1.0
 */
public class ThreadPoolUtils {

    private static volatile ThreadPoolUtils instance = null;
    private ThreadPoolExecutor executor = new ThreadPoolExecutor(4,
            16,
            60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(1024));

    /**
     * @Author chl
     * @Description T573
     * @Date 16:30 2023/8/30
     * @param corePoolSize 默认线程数
     * @return maximumPlloSize 最大线程数
     **/
    public static ExecutorService dataMigrateExecutor = new ThreadPoolExecutor(20, 100,
            1000, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(5 * 1024),
            new BasicThreadFactory.Builder().namingPattern("executor-%d").build(),
            new ThreadPoolExecutor.AbortPolicy());

    private ThreadPoolUtils() {
    }

    public static ThreadPoolUtils getInstance() {
        if (null == instance) {
            synchronized (ThreadPoolUtils.class) {
                if (null == instance) {
                    instance = new ThreadPoolUtils();
                }
            }
        }
        return instance;
    }


    public void executor(Runnable runnable) {
        executor.execute(runnable);
    }

    public void shutdown() {
        executor.shutdown();
    }
}
